var path = require('path');
var Webpack = require('webpack');
var PurifyCss = require('purifycss-webpack');
var glob = require('glob-all');
var ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
const extractLess = new ExtractTextWebpackPlugin('stylesheets/[name]-two.css');
const HtmlWebpackPlugin =require('html-webpack-plugin');

module.exports = {
    entry: {
        app: './src/app.js'
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        // publicPath: './dist/',
        filename: '[name].bundle.js',
        chunkFilename: '[name].bundle.js'
    },
    resolve: {
        alias: {
            jquery$: path.resolve(__dirname, 'src/lib/jquery.min.js')
        }
    },
    module: {
        rules: [{
                test: /\.less$/,
                use: ExtractTextWebpackPlugin.extract({
                    fallback: {
                        loader: 'style-loader',
                        options: {
                            singleton: true,
                            // transform: './css.tranform.js'
                        }
                    },
                    use: [{
                            loader: 'css-loader',
                            options: {
                                importLoaders: 2
                                // minimize: true,
                                // modules: true,
                                // localIdentName: '[path][name]_[local]_[hash:base64:5]'
                            }
                        },
                        {
                            loader: 'postcss-loader',
                            options: {
                                ident: 'postcss',
                                plugins: [
                                    // require('autoprefixer')(),
                                    require('postcss-sprites')({
                                        spritePath: 'dist/accets/imgs/',
                                        retina: true
                                    }),
                                    require('postcss-cssnext')()
                                ]
                            }
                        },
                        {
                            loader: 'less-loader'
                        }
                    ]
                }),
            },
            {
                test: /\.js$/,
                use: [{
                    loader: 'babel-loader',
                    options: {
                        presets: ['env'],
                        plugins: ['lodash']
                    }
                }]
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        name: '[name].min.[ext]',
                        limit: 100,
                        outputPath: 'accets/imgs',
                        useRelativePath: true,
                    }
                }, {
                    loader: 'img-loader',
                    options: {
                        pngquant: {

                        }
                    }
                }]
            }
        ]
    },
    plugins: [
        extractLess,
        new PurifyCss({
            paths: glob.sync([
                path.join(__dirname, './*.html'),
                path.join(__dirname, './src/*.js'),
            ])
        }),
        // new Webpack.ProvidePlugin({
        //     $: 'jquery'
        // }),
        new HtmlWebpackPlugin({
            filename:'index.html',
            template:'./index.html'
        }),
        new Webpack.optimize.UglifyJsPlugin()
    ]
}